import SwiftUI

struct CartBottomBar: View {
    @Binding var isAllSelected: Bool
    var totalPrice: Double
    var totalCount: Int
    var isEditing: Bool
    var onAllSelectToggle: () -> Void
    var onDeleteSelected: (() -> Void)?
    var onCheckout: (() -> Void)?
    
    var body: some View {
        VStack(spacing: 0) {
            Divider()
            
            HStack(spacing: 12) {
                // 全选按钮
                Button(action: onAllSelectToggle) {
                    HStack(spacing: 8) {
                        Image(systemName: isAllSelected ? "checkmark.circle.fill" : "circle")
                            .foregroundColor(isAllSelected ? .blue : .gray)
                        
                        Text("全选")
                            .font(.system(size: 14))
                            .foregroundColor(.primary)
                    }
                }
                
                Spacer()
                
                if isEditing {
                    // 编辑模式下显示删除按钮
                    Button(action: {
                        onDeleteSelected?()
                    }) {
                        Text("删除")
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 120, height: 40)
                            .background(Color.red)
                            .cornerRadius(20)
                    }
                } else {
                    // 正常模式下显示价格和结算按钮
                    VStack(alignment: .trailing, spacing: 2) {
                        Text("合计: ¥\(String(format: "%.2f", totalPrice))")
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.primary)
                        
                        Text("不含运费")
                            .font(.system(size: 12))
                            .foregroundColor(.gray)
                    }
                    
                    Button(action: {
                        onCheckout?()
                    }) {
                        Text("结算(\(totalCount))")
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 120, height: 40)
                            .background(Color.blue)
                            .cornerRadius(20)
                    }
                    .disabled(totalCount == 0)
                    .opacity(totalCount == 0 ? 0.6 : 1)
                }
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 10)
            .background(Color.white)
            .padding(.bottom, 20) // 为底部安全区域添加额外的padding
        }
    }
}

struct CartBottomBar_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            Spacer()
            CartBottomBar(
                isAllSelected: .constant(true),
                totalPrice: 299.99,
                totalCount: 3,
                isEditing: false,
                onAllSelectToggle: {}
            )
        }
        .previewLayout(.sizeThatFits)
    }
}